package com.example.asus.testskip.DGPhone;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Handler;
import android.os.Message;
import android.support.v4.internal.view.SupportMenu;
import android.support.v4.view.ViewCompat;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.WindowManager;

import com.example.asus.testskip.R;

/* renamed from: dg.ca.com.DGPhone.MarqueeView */
public class MarqueeView extends SurfaceView implements Callback {
    public static final int ROLL_OVER = 100;
    private int ShadowColor;
    public int currentX;
    private SurfaceHolder holder;
    /* access modifiers changed from: private */
    public int mBackgroundColor;
    public Context mContext;
    /* access modifiers changed from: private */
    public int mDirection;
    Handler mHandler;
    /* access modifiers changed from: private */
    public boolean mIsRepeat;
    OnMargueeListener mOnMargueeListener;
    /* access modifiers changed from: private */
    public int mSpeed;
    private int mStartPoint;
    private int mTextColor;
    /* access modifiers changed from: private */
    public TextPaint mTextPaint;
    private float mTextSize;
    private MarqueeViewThread mThread;
    /* access modifiers changed from: private */
    public String margueeString;
    public int sepX;
    /* access modifiers changed from: private */
    public int textHeight;
    /* access modifiers changed from: private */
    public int textWidth;

    /* renamed from: dg.ca.com.DGPhone.MarqueeView$MarqueeViewThread */
    class MarqueeViewThread extends Thread {
        private SurfaceHolder holder;
        public boolean isRun = true;

        public MarqueeViewThread(SurfaceHolder holder2) {
            this.holder = holder2;
        }

        public void onDraw() {
            try {
                synchronized (this.holder) {
                    if (TextUtils.isEmpty(MarqueeView.this.margueeString)) {
                        Thread.sleep(1000);
                        return;
                    }
                    Canvas canvas = this.holder.lockCanvas();
                    int paddingLeft = MarqueeView.this.getPaddingLeft();
                    int paddingTop = MarqueeView.this.getPaddingTop();
                    int contentWidth = (MarqueeView.this.getWidth() - paddingLeft) - MarqueeView.this.getPaddingRight();
                    int centeYLine = paddingTop + (((MarqueeView.this.getHeight() - paddingTop) - MarqueeView.this.getPaddingBottom()) / 2);
                    if (MarqueeView.this.mDirection == 0) {
                        if (MarqueeView.this.currentX <= (-MarqueeView.this.textWidth)) {
                            if (!MarqueeView.this.mIsRepeat) {
                                MarqueeView.this.mHandler.sendEmptyMessage(100);
                            }
                            MarqueeView.this.currentX = contentWidth;
                        } else {
                            MarqueeView.this.currentX -= MarqueeView.this.sepX;
                        }
                    } else if (MarqueeView.this.currentX >= contentWidth) {
                        if (!MarqueeView.this.mIsRepeat) {
                            MarqueeView.this.mHandler.sendEmptyMessage(100);
                        }
                        MarqueeView.this.currentX = -MarqueeView.this.textWidth;
                    } else {
                        MarqueeView.this.currentX += MarqueeView.this.sepX;
                    }
                    if (canvas != null) {
                        canvas.drawColor(MarqueeView.this.mBackgroundColor);
                        Log.i("我的信息", "margueeString:" + canvas + ";" + MarqueeView.this.margueeString + ";" + centeYLine + ";" + MarqueeView.this.getContext());
                        canvas.drawText(MarqueeView.this.margueeString, (float) MarqueeView.this.currentX, (float) ((MarqueeView.dip2px(MarqueeView.this.getContext(), (float) MarqueeView.this.textHeight) / 2) + centeYLine), MarqueeView.this.mTextPaint);
                        this.holder.unlockCanvasAndPost(canvas);
                    }
                    int b = (MarqueeView.this.textWidth / MarqueeView.this.margueeString.trim().length()) / MarqueeView.this.sepX;
                    Thread.sleep((long) (MarqueeView.this.mSpeed / b == 0 ? 1 : MarqueeView.this.mSpeed / b));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void run() {
            while (this.isRun) {
                onDraw();
            }
        }
    }

    /* renamed from: dg.ca.com.DGPhone.MarqueeView$OnMargueeListener */
    public interface OnMargueeListener {
        void onRollOver();
    }

    public MarqueeView(Context context) {
        this(context, null);
    }

    public MarqueeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MarqueeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mTextSize = 100.0f;
        this.mTextColor = SupportMenu.CATEGORY_MASK;
        this.mBackgroundColor = -1;
        this.textWidth = 0;
        this.textHeight = 0;
        this.ShadowColor = ViewCompat.MEASURED_STATE_MASK;
        this.currentX = 0;
        this.sepX = 5;
        this.mHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 100:
                        MarqueeView.this.stopScroll();
                        if (MarqueeView.this.mOnMargueeListener != null) {
                            MarqueeView.this.mOnMargueeListener.onRollOver();
                            return;
                        }
                        return;
                    default:
                        return;
                }
            }
        };
        this.mContext = context;
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeView, defStyleAttr, 0);
        this.mTextColor =a.getColor( a.getIndex(1), Color.GRAY);
        this.mTextSize = a.getDimension(a.getIndex(2), 48.0f);
        this.mBackgroundColor = a.getColor(a.getIndex(0), ViewCompat.MEASURED_STATE_MASK);
        this.mIsRepeat = a.getBoolean(a.getIndex(3), false);
        this.mStartPoint = a.getInt(a.getIndex(4), 0);
        this.mDirection = a.getInt(a.getIndex(5), 0);
        this.mSpeed = a.getInt(a.getIndex(6), 20);

        int count = a.getIndexCount();
        //通过for循环遍历typedArray

        a.recycle();
        this.holder = getHolder();
        this.holder.addCallback(this);
        this.mTextPaint = new TextPaint();
        this.mTextPaint.setFlags(1);
        this.mTextPaint.setTextAlign(Align.LEFT);
    }

    public void setText(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            measurementsText(msg);
        }
    }

    /* access modifiers changed from: protected */
    public void measurementsText(String msg) {
        this.margueeString = msg;
        this.mTextPaint.setTextSize(this.mTextSize);
        this.mTextPaint.setColor(this.mTextColor);
        this.mTextPaint.setStrokeWidth(0.5f);
        this.mTextPaint.setFakeBoldText(true);
        this.textWidth = (int) this.mTextPaint.measureText(this.margueeString);
        this.textHeight = (int) this.mTextPaint.getFontMetrics().bottom;
        int width = ((WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
        if (this.mStartPoint == 0) {
            this.currentX = 0;
        } else {
            this.currentX = (width - getPaddingLeft()) - getPaddingRight();
        }
    }

    public void surfaceCreated(SurfaceHolder holder2) {
        this.holder = holder2;
    }

    public void surfaceChanged(SurfaceHolder holder2, int format, int width, int height) {
        if (this.mThread != null) {
            this.mThread.isRun = true;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder2) {
        if (this.mThread != null) {
            this.mThread.isRun = false;
        }
    }

    public void startScroll() {
        if (this.mThread == null || !this.mThread.isRun) {
            this.mThread = new MarqueeViewThread(this.holder);
            this.mThread.start();
        }
    }

    public void stopScroll() {
        if (this.mThread != null) {
            this.mThread.isRun = false;
            this.mThread.interrupt();
        }
        this.mThread = null;
    }

    public static int dip2px(Context context, float dpValue) {
        return (int) ((dpValue * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public void reset() {
        int contentWidth = (getWidth() - getPaddingLeft()) - getPaddingRight();
        if (this.mStartPoint == 0) {
            this.currentX = 0;
        } else {
            this.currentX = contentWidth;
        }
    }

    public void setOnMargueeListener(OnMargueeListener mOnMargueeListener2) {
        this.mOnMargueeListener = mOnMargueeListener2;
    }
}
